//
//  parser.c
//  
//  parser
//
//  Created by Pei Liu on 10/25/11.
//  Copyright 2011 FOR CS 440. All rights reserved.
//

#include "parser.h"

/*change id to number of procedure and @*/
char* changeid(char* in)
{
	/*add distinguisher*/
	char* temp;
	temp = (char*)malloc((strlen(in) + strlen(proname) + 2) * sizeof(char));
	
	temp = strcat(temp, in);
	temp = strcat(temp, "@");
	temp = strcat(temp, proname);

	return temp;
}

/* store opcode into the code store*/
void emit(int opcode, int operand1, int operand2)
{
    store_int(opcode, cur_cs++, cs);
    store_int(operand1, cur_cs++, cs);
    store_int(operand2, cur_cs++, cs);
    
}
/*back patch a certain position*/
void bpatch(int index, int newval)
{
    store_int(newval, index, cs);
}
/*add header*/
void addheader(FILE* fout, int cs_size, int beginstr, int strused, int enterpoint)
{
    if (fout == NULL)
        fout = stdout;
    fprintf(fout, "%d\n%d\n%d\n%d\n", cs_size, beginstr, strused, enterpoint);
}
/*out put the string char by char*/
void strlisttrav(FILE* fout, char** strlist, int strNbr)
{
    if (fout == NULL)
        fout = stdout;
    
    int i = 0;
    for(i = 0; i < strNbr; i++)
    {
        int j = 0;
        for (j = 0; j < strlen(strlist[i]); j++)
        {
            fprintf(fout, "%d\n", (int)strlist[i][j]);
        }
        fprintf(fout, "%d\n", 0);
    }   
}